package com.qianli.user.service.impl;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.xjd.user.client.enums.FrozenCodeEnum;
import com.fqgj.xjd.user.dao.UserAuthStatusDao;
import com.fqgj.xjd.user.dao.UserFrozenCodeMapDao;
import com.fqgj.xjd.user.entity.UserAuthStatusEntity;
import com.fqgj.xjd.user.entity.UserFrozenCodeMapEntity;
import com.qianli.user.enums.UserAuthStatusEnum;
import com.qianli.user.job.UnFrozenUserJOb;
import com.qianli.user.service.UserFrozenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * name:用户冷冻
 * auth:ting.li@qianli-inc.com
 * date:2018/1/19 10:13
 * version: v1.0.0
 **/
@Service
public class UserFrozenServiceImpl implements UserFrozenService{
    private static final Logger LOGGER = LoggerFactory.getLogger(UnFrozenUserJOb.class);

    private static final int PAGE_SIZE = 500;

    @Autowired
    private UserFrozenCodeMapDao userFrozenCodeMapDao;
    @Autowired
    private UserAuthStatusDao userAuthStatusDao;

    @Override
    public void unFrozenUsers() {

        List<UserFrozenCodeMapEntity> frozenCodeMaps = null;
        //人脸识别拒绝,借款被拒冷冻,账号冷冻,额度评估冷冻
        Date now = new Date();
        while (true){
            frozenCodeMaps = userFrozenCodeMapDao.selectUserFrozenCodeMapByCondition(DateUtil.getDateTime(now),
                    null,PAGE_SIZE);
            if(CollectionUtils.isEmpty(frozenCodeMaps)){
                break;
            }
            for(UserFrozenCodeMapEntity entity:frozenCodeMaps){
                entity.setDeleted(true);
                userFrozenCodeMapDao.updateByPrimaryKey(entity);
                //额度评估解冻，发短信
                if(!FrozenCodeEnum.EVALUATE_REJECT.getCode().equals(entity.getFrozenCode())){
                    sendMsg(entity.getUserCode());
                }
                //解冻第一轮风控
                if(FrozenCodeEnum.FIRST_RISK_FROZEN.getCode().equals(entity.getFrozenCode())){
                    UserAuthStatusEntity userAuthStatusEntity = userAuthStatusDao.getUserAuthStatusByUserCode(entity.getUserCode());
                    if(userAuthStatusEntity.getFirstRoundRiskStatus() == UserAuthStatusEnum.REJECT.getCode()){
                        userAuthStatusEntity.setFirstRoundRiskStatus(UserAuthStatusEnum.INIT.getCode());
                        userAuthStatusDao.updateByPrimaryKey(userAuthStatusEntity);
                    }
                }

            }
            LOGGER.info("=========== 解冻任务结束 ===============");
        }
    }
    private void sendMsg(String userCode) {

//        Response<UserRO> response = userQueryService.queryUserByUserCode(userCode);
//        if(!response.isSuccess() || null == response.getData()){
//            LOGGER.info("userCode:{} is not exists",userCode);
//            return;
//        }
//        UserRO user = response.getData();
//        SingleSmsSendRequestRO requestRO = new SingleSmsSendRequestRO();
//        requestRO.setPhone(user.getUserAccess().getMobile());
//        requestRO.setBizCode("CASHMAMA_0005");
//        Map<String,String> params = Maps.newHashMap();
//        params.put("name",user.getCustomer().getName());
//        requestRO.setReplaceParam(params);
//        smsMsgService.send(requestRO);
    }
}
